const _ = require('../utils')

_.create('actions', {
  data: {
    bordered: true,
  },

  properties: {
    title: {
      type: String,
      value: '请选择...',
    },
    onTap: {
      type: Function,
      value: () => {},
    },
  },

  lifetimes: {
    attached() {
      this.popup = this.selectComponent('#mp-popup')
    },
  },

  methods: {
    show(options) {
      const opt = Object.assign({ items: [] }, options)

      this.setData(opt)
      this.popup.show()
      return this
    },

    close() {
      this.popup.close()
    },

    onTap(e) {
      const { index } = e.target.dataset
      const item = this.data.items[index]
      this.data.onTap({ index, item })
      this.popup.close()
    },
  },
})
